package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Settings;
import com.tanhua.dubbo.mapper.SettingsMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SettingApiImpl implements SettingApi {
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 通过id查询通知设置
     * @param id
     * @return
     */
    @Override
    public Settings findByUserId(Long id) {
        //构建查询条件
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<Settings>();
        queryWrapper.eq("user_id",id);
      return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 用户通知设置
     * @param settings
     */
    @Override
    public void setSettings(Settings settings) {
        //查询用户通知设置
        Settings settingsInDb = findByUserId(settings.getUserId());
        //存在则更新
        if (null!=settingsInDb){
            settings.setId(settingsInDb.getId());
            settingsMapper.updateById(settings);
        }
        else {
            //不存在则添加
            settingsMapper.insert(settings);
        }

    }
}
